//
//  NSString+EmojiText.m
//  YunShouHu
//
//  Created by WangQiang on 16/7/12.
//  Copyright © 2016年 WangQiang. All rights reserved.
//

#import "NSString+EmojiAttribute.h"
#import "ChatEnumType.h"

#import "EmojiAttachment.h"
@implementation NSString (EmojiAttribute)
-(NSAttributedString *)decodeEmojiAttribute{
    __block  NSMutableAttributedString *attrbute = [[NSMutableAttributedString alloc] initWithString:self attributes:@{NSFontAttributeName : TextFont ,NSForegroundColorAttributeName : [UIColor lightGrayColor]}];
    if(self.length <= 0)return attrbute;
    //用正则表达式匹配表情字符串
    NSRegularExpression *regx = [NSRegularExpression regularExpressionWithPattern:@"\\[[\\u4e00-\\u9fa5]+\\]" options:NSRegularExpressionCaseInsensitive error:nil];
    
    NSArray *reults =  [regx matchesInString:self options:NSMatchingReportCompletion range:NSMakeRange(0, self.length)];
    [reults enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSTextCheckingResult *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *encodeStr = [self substringWithRange:obj.range];
        EmojiEmotion *emotion = [EmojiEmotion emojiEmotionWithEncode:encodeStr];
        if(emotion){
            [attrbute replaceCharactersInRange:obj.range withAttributedString:[EmojiAttachment emojiAttachmentAttributeStringWithEmotion:emotion]];
        }
    }];
    return attrbute;
}
@end
